package com.corpdata.app.contract.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.corpdata.app.contract.entity.ContractInfo;
import com.corpdata.app.contract.service.ContractInfoService;
import com.corpdata.app.contract.service.ContractPartitionService;
import com.corpdata.app.customer.entity.CustomerTeam;
import com.corpdata.app.customer.service.CustomerTeamService;
import com.corpdata.app.project.service.ProjectInfoService;
import com.corpdata.common.domain.DataGridRequestDTO;
import com.corpdata.common.result.Result;
import com.corpdata.common.upload.service.SysAttachmentService;
import com.corpdata.common.utils.CorpdataUtil;
import com.corpdata.core.base.BaseController;
import com.corpdata.system.log.WebLog;

/**
 * 合同表
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-07-02 16:38:20
 */
 
@Controller
@RequestMapping("app/contractInfo")  //
public class ContractInfoController extends BaseController{

	@Autowired
	private ContractInfoService contractInfoService;
	
	@Autowired
	private ProjectInfoService projectInfoService;
	
	@Autowired
	private CustomerTeamService teamServ;
	
	@Autowired
	private ContractPartitionService cpServ;
	
	@Autowired
	private SysAttachmentService sysAttachmentService;
	
	//新增页面
	@GetMapping("/add/{id}_{fkTeam}")
	public String add(@PathVariable("id") String id,@PathVariable("fkTeam") String fkTeam,ModelMap map){
		map.put("project", projectInfoService.findById(id));
		map.put("fkTeam", fkTeam);
		return "app/contract/contract_add";
	}
	//新增合同细节
	@GetMapping("/addDetails/{id}_{fkTeam}")
	public String addDetails(@PathVariable("id") String id,@PathVariable("fkTeam") String fkTeam,ModelMap map){
		if(id.equals("all")){
			CustomerTeam t = teamServ.findById(fkTeam);
			id = t.getFkProject();
		}
		map.put("project", projectInfoService.findById(id));
		map.put("id", CorpdataUtil.getUUID());
		map.put("fkTeam", fkTeam);
		return "app/contract/contract_add_details";
	}
	//新增合同审批
	@GetMapping("/addApproval")
	public String addApproval(){
		return "app/contract/contract_add_approval";
	}
	//保存数据
	@WebLog()
	@ResponseBody
	@RequestMapping("/save")
	public Result save(ContractInfo record,String ysList,String gwList,String fileId){
			JSONArray reList = JSONArray.parseArray(ysList.replaceAll("&quot;", "\""));
			JSONArray staList = JSONArray.parseArray(gwList.replaceAll("&quot;", "\""));
			
		return contractInfoService.insert(record,reList,staList,fileId);
	}
	
	//编辑页面
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id,ModelMap map){
		map.put("record", contractInfoService.findById(id));
		return "app/contract/contractInfo_edit";
	}
	
	//更新数据
	@WebLog()
	@ResponseBody
	@RequestMapping("/update")
	public Result update(ContractInfo record){
		return contractInfoService.update(record);
	}
	
	//删除
	@WebLog()
	@ResponseBody
	@PostMapping("/delete")
	public Result delete(String id){
		return contractInfoService.deleteById(id);
	}
	
	//批量删除
	@WebLog()
	@ResponseBody
	@PostMapping("/deletes")
	public Result deletes(@RequestParam("ids[]") String[] ids){
		return contractInfoService.deleteByIds(ids);
	}
	
	//详情页面
	@GetMapping("/details/{id}")
	public String details(ModelMap map,@PathVariable("id") String id){
		ContractInfo info =  contractInfoService.findById(id);
		map.put("contract",info);
		map.put("team",teamServ.findById(info.getFkTeam()));
		map.put("sysAttachments", sysAttachmentService.findByDocUnid(id));
	    return "app/contract/detail/contractInfo_details";
	}
	
	//列表页面
	@GetMapping("/list/{teamId}_{fkProject}")
	public String list(ModelMap map,@PathVariable("teamId") String teamId,@PathVariable("fkProject") String fkProject){
		map.put("teamId", teamId);
		map.put("fkProject", fkProject);
	    return "app/contract/contractInfo_list";
	}
	
	//列表数据
	@ResponseBody
	@RequestMapping("/listdata")
	public String findByPage(DataGridRequestDTO dgRequest){
		return contractInfoService.findByPage(dgRequest);
	}
	
	//详情中心页
	@GetMapping("/detailCenter/{id}")
	public String setBasic(ModelMap map,@PathVariable("id") String id){
		map.put("contractId", id);
	    return "app/contract/detail/contract_center";
	}
	
	//新增续签合同
	@GetMapping("/toAddReNewContract/{fkProject}_{fkTeam}_{contractId}")
	public String toAddReNewContract(@PathVariable("fkProject") String fkProject,@PathVariable("fkTeam") String fkTeam,@PathVariable("contractId") String contractId,ModelMap map){
		String id =  CorpdataUtil.getUUID();
		map.put("project", projectInfoService.findById(fkProject));
		map.put("c", contractInfoService.findById(contractId));
		map.put("id",id);
		map.put("fkTeam", fkTeam);
		//将前合同下分区和工位关联到合同
		cpServ.insertContractPartition(contractId,id);
		return "app/contract/contract_re_add";
	}
	
	//跳转合同终止页面
	@GetMapping("/toEndContract/{id}")
	public String toEndContract(ModelMap map,@PathVariable("id") String id){
		map.put("contract",contractInfoService.findById(id) );
	    return "app/contract/contract_end";
	}
	
	//合同管理中心页
	@GetMapping("/index")
	public String index(){
	    return "app/contract/index";
	}
	//终止合同
	@WebLog()
	@ResponseBody
	@RequestMapping("/endContract")
	public Result endContract(String contractId,String bills,String delBillStatus,String endDateStatus,String endRemark,String realEndDate,String reason){
		JSONArray billList = JSONArray.parseArray(bills.replaceAll("&quot;", "\""));
		return contractInfoService.endContract(contractId,billList,delBillStatus,endDateStatus,endRemark,realEndDate,reason);
	}
	
	//财务管理中心页
	@GetMapping("/center")
	public String center(){
	    return "app/contract/finance/index";
	}
	
	//验证合同编号
	@ResponseBody
	@RequestMapping("/checkContractNum")
	public int checkContractNum(String contractNum){
		return contractInfoService.checkContractNum(contractNum);
	}
	
	//获取合同到期天数
	@ResponseBody
	@RequestMapping("/getDaoqiDays")
	public int getDaoqiDays(String fkTeam){
		return contractInfoService.getDaoQiDays(fkTeam);
	}
}
